﻿using System.Collections.ObjectModel;
using KinonekoSoftware.JuXiaoYouDB.Core;
using KinonekoSoftware.JuXiaoYouDB.Documents.General;
using KinonekoSoftware.JuXiaoYouDB.Internals;
using KinonekoSoftware.JuXiaoYouDB.Objects;
using KinonekoSoftware.Studio;

namespace KinonekoSoftware.JuXiaoYouDB.SubSystem.Categories
{
    [DataSource(Database.Sqlite, CollectionName = "Types", FileName = FileNames.InstanceTypeDB)]
    public sealed class InstanceType : NameObject, IComparable<InstanceType>
    {
        private bool _isFavorite;

        public int CompareTo(InstanceType other)
        {
            var a = (IsFavorite ? 200 : 0) + Index;
            var b = other is null ? -1 : (other.IsFavorite ? 200 : 0) + other.Index;
            return a - b;
        }

        public static IEnumerable<InstanceType> GenerateDefaultTypes()
        {
            return new[]
            {
                new InstanceType { Id = ID.Get() }
            };
        }

        public static InstanceType Create(string name, IEnumerable<ModuleGroupDefinition> definitions, IEnumerable<ModuleElement> properties)
        {
            return new InstanceType
            {
                Id          = ID.Get(),
                Name        = name,
                Index       = 0,
                Definitions = new ObservableCollection<ModuleGroupDefinition>(definitions),
                Properties  = new ObservableCollection<ModuleElement>(properties),
            };
        }

        public static ModuleGroupDefinition Definition(string name, string intro, IEnumerable<ModuleElementType> types)
        {
            return new ModuleGroupDefinition
            {
                Id          = ID.Get(),
                Name        = name,
                Index       = 0,
                Intro       = intro,
                Definitions = new ObservableCollection<ModuleElementType>(types),
            };
        }

        public static StringModuleElement String(string name) => new StringModuleElement { Id = ID.GetShortID(), Name = name };
        
        public int Index { get; set; }

        public bool IsFavorite
        {
            get => _isFavorite;
            set => SetValue(ref _isFavorite, value);
        }

        public ObservableCollection<ModuleGroupDefinition> Definitions { get; set; }
        public ObservableCollection<ModuleElement>         Properties  { get; set; }
    }
}